<?php

require_once "spCaption.php";

class spTable extends spElement
{
	private $style_td = null;

	public function __construct( $param=null )
	{
		parent::__construct( "table", $param );

		if ( isset($param["caption"]) )
			$this->setCaption( $param["caption"] );

		if ( isset($param["style_td"]) )
			$this->style_td = $param["style_td"];
		
		// not both x and y must be set to valid
		if ( isset($param["y"]) && isset($param["x"]) )
		{
			$x = $param["x"];
			$y = $param["y"];
			$this->createBody( $x, $y );
		}
		
		if ( isset($param["header"]) )
			$this->createHeader( $param["header"] );
	}
	
	public function setCaption( $text )
	{
		parent::insertChild( new spCaption(array(
			"content" => $text
		)));
	}
	
	public function createHeader( $ar )
	{
		$thead = new spTableHeader;
		$this->insertChild( $thead );	// make sure we insert this as the first row
		
		$thead_tr = new spTableRow;
		$thead->addChild( $thead_tr );
		
		foreach ( $ar as $a )
		{
			$thead_tr->addTableHeaderData( $a );
		}
		
		return $thead;
	}
	
	public function createBody( $x, $y )
	{
		$tbody = new spTableBody;
		$this->addChild( $tbody );
	
		for ( $iy=0; $iy<$y; $iy++ )
		{
			$row = new spTableRow;
			$tbody->addChild( $row );
		
			for ( $ix=0; $ix<$x; $ix++ )
			{
				$cell = new spTableData;
				
				if ( $this->style_td!=null )
					$cell->addStyle( $this->style_td );
				
				$row->addChild( $cell );
			}
		}
	}
	
	public function getCell( $x, $y )
	{
		$tbody = parent::findTag( "tbody" );
	
		$row = $tbody->findTagExt( "tr", $y );
		if ( $row==null )
			return false;
		
		$cell = $row->findTagExt( "td", $x );
		if ( $cell==null )
			return false;		
	
		return $cell;
	}
	
	public function setCell( $x, $y, $data )
	{
		$cell = $this->getCell( $x, $y );
		
		if ( $data instanceof spElement )
			$cell->addChild( $data );
		else
			$cell->setContent( $data );
	}
	
	public function addCellStyle( $x, $y, $styleName, $style )
	{
		$cell = $this->getCell( $x, $y );	
		$cell->addStyle( $styleName, $style );
	}
	
	public function addRowText( $arCells )
	{
		$header = new spTableRow;
		parent::addChild( $header );
	
		foreach ( $arCells as $c )
			$header->addChild( new spTableData( array("content"=>$c) ) );
			
		return $header;
	}	
	
	public function addRow( $arCells=array() )
	{
		$row = new spTableRow;
		parent::addChild( $row );
		
		foreach ( $arCells as $c )
		{
			$td = new spTableData;
			$row->addChild( $td );
			
			if ( $c && $c instanceof spElement )
				$td->addChild( $c );
			else
				$td->setContent( $c );
		}
		
		return $row;
	}	
	
	public function createFromArray( $ar )
	{
		// validate that we have something to process
		if ( !count($ar) )
			return;
			
		////////////////////////////////////////////////////////
		// create a thead
		////////////////////////////////////////////////////////
		
		$thead = new spTableHeader;
		$this->addChild( $thead );
		
		$thead_tr = new spTableRow;
		$thead->addChild( $thead_tr );
		
		foreach ( $ar[0] as $k => $v )
		{
			$thead_tr->addTableHeaderData( $k );
		}
		
		////////////////////////////////////////////////////////
		// create a tbody
		////////////////////////////////////////////////////////
		
		$tbody = new spTableBody;
		$this->addChild( $tbody );
		
		foreach ( $ar as $a )
		{
			$tbody_tr = new spTableRow;
			$tbody->addChild( $tbody_tr );
		
			$count = 0;
			foreach ( $a as $k => $v )
			{
				$cell = $this->addTableData( array(
					"tablerow" 	=> $tbody_tr,
					"index" 	=> $count,
					"key"		=> $k,
					"data"		=> $v 
				));

				if ( $this->style_td!=null )
					$cell->addStyle( $this->style_td );
					
				$count++;
			}
		}
	}
	
	// override this to modify the data or create your own table data
	public function addTableData( $param )
	{
		$row 	= $param["tablerow"];
		$data 	= $param["data"];
		
		return $row->addTableData( $data );
	}

}


?>